﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Test.TestCase;

namespace USBListenerWindowsForms
{
    public partial class Form1 : Form
    {
        DriveDetector detector = new DriveDetector();
        public Form1()
        {
            InitializeComponent();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            detector.WndProc(m.HWnd, m.Msg, m.WParam, m.LParam);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var drivers = DriveDetector.GetDeviceRootDir("U");
            if (drivers != null && drivers.Count > 0)
            {
                label1.BeginInvoke(new Action(() =>
                {
                    label1.Text = "USB设备已连接";
                }));
            }


            detector.DeviceArrived += (o, evt) =>
            {
                label1.BeginInvoke(new Action(() =>
                {
                    label1.Text = "USB设备已连接";
                }));
            };

            detector.DeviceRemoved += (o, evt) =>
            {
                label1.BeginInvoke(new Action(() =>
                {
                    label1.Text = "USB设备断开连接";
                }));
            };
        }
    }
}
